﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Media.Imaging;
using FluxJpeg.Core;
using FluxJpeg.Core.Encoder;
using WebCamView.Code;

namespace WebCamView
{
    public partial class Home : Page
    {
        ServiceReference1.Service1Client server;
        private CaptureSource _captureSource;
        
        public Home()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            server = new ServiceReference1.Service1Client();
            server.DoWorkCompleted += new EventHandler<ServiceReference1.DoWorkCompletedEventArgs>(onWorkCompleted);
            server.LoadImageCompleted += new EventHandler<ServiceReference1.LoadImageCompletedEventArgs>(onLoadCompleted);
            server.LoadImageMessageCompleted += new EventHandler<ServiceReference1.LoadImageMessageCompletedEventArgs>(onLoadMessageImageCompleted);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            VideoSources.ItemsSource = CaptureDeviceConfiguration.GetAvailableVideoCaptureDevices();
            AudioSources.ItemsSource = CaptureDeviceConfiguration.GetAvailableAudioCaptureDevices();
            _captureSource = new CaptureSource();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        
        }

        private void DoWork_Click(object sender, RoutedEventArgs e)
        {
            server.DoWorkAsync();
        }

        private void onWorkCompleted(object target, ServiceReference1.DoWorkCompletedEventArgs args)
        {
            DoWorkText.Text = (string)args.Result;
        }

        private void TestBytes_Click(object sender, RoutedEventArgs e)
        {
            server.SaveImageAsync(new byte[] {1, 2, 3, 4, 5});
            TestBytesResponse.Text = "sent!";
        }

        private void onLoadCompleted(object sender, ServiceReference1.LoadImageCompletedEventArgs args){
            byte[] bytes = args.Result;
            if (bytes != null)
            {
                LoadBytesResponse.Text = "count: " + bytes.Length;
            }
            else
            {
                LoadBytesResponse.Text = "empty...";
            }
        }

        private void LoadBytes_Click(object sender, RoutedEventArgs e)
        {
            server.LoadImageAsync();
        }

        private void SendMessage_Click(object sender, RoutedEventArgs e)
        {
            server.SaveImageMessageAsync(new ServiceReference1.ImageData(){Content=new byte[] {1, 2, 3}, Height=100, Width=100} );
            SendMessageResponse.Text = "sent";
        }

        private void LoadMessage_Click(object sender, RoutedEventArgs e)
        {
            server.LoadImageMessageAsync();
        }

        private void onLoadMessageImageCompleted(object sender, ServiceReference1.LoadImageMessageCompletedEventArgs args)
        {
            ServiceReference1.ImageData message = args.Result;
            if (message != null && message.Content != null)
            {
                LoadMessageResponse.Text = message.Width + ":" + message.Height;
            }
            else
            {
                LoadMessageResponse.Text = "empty...";
            }
        }

        private void takeSnapshot_Click(object sender, RoutedEventArgs e)
        {
            if (_captureSource != null)
            {
                _captureSource.AsyncCaptureImage((snapImage) =>
                {
                    using (MemoryStream _ms = new MemoryStream())
                    {
                        ImageTools.EncodeJpeg(snapImage, _ms);
                        _ms.Flush();
                        var imageBytes = _ms.ToArray();//.ToList().GetRange(0, 100).ToArray();
                        server.SaveImageAsync(imageBytes);
                        SendMessageResponse.Text = "sent image! " + imageBytes.Length;
                    }
                });
            }
        }

        private void loadSnapshot_Click(object sender, RoutedEventArgs e)
        {

        }

        private void startCapture_Click(object sender, RoutedEventArgs e)
        {
            if (_captureSource != null)
            {
                _captureSource.Stop();

                _captureSource.VideoCaptureDevice = (VideoCaptureDevice)VideoSources.SelectedItem;
                _captureSource.AudioCaptureDevice = (AudioCaptureDevice)AudioSources.SelectedItem;

                VideoBrush vidBrush = new VideoBrush();
                vidBrush.SetSource(_captureSource);

                videoRect.Fill = vidBrush;

                if (CaptureDeviceConfiguration.AllowedDeviceAccess ||
                  CaptureDeviceConfiguration.RequestDeviceAccess())
                {
                    _captureSource.Start();
                }
            }
        }

        private void endCapture_Click(object sender, RoutedEventArgs e)
        {
            if (_captureSource != null)
            {
                _captureSource.Stop();
            }
        }
    }
}